/*
 * COMP6471 Project3
 * 
 * Assignment 3: Due 10:00 Thursday 27 October 2011. 
 * Submit a .tar.gz file with your Java program, list of games files, and a 2-page description of your domain model to Project 3 of electronic submission system. 
 * First version of an interactive game in Java for Kenken. It supports a minimal game: a user can select a game, enter values into grid locations, and the systems reports success or failure when the grid is full. 
 * The game is selected from a list of games that you provide for the system. 
 * Focus on 4-by-4 games first, but you should support 6-by-6 games too. 
 * Concentrate on a architecture and design that is easily extensible.
 * 
 * This file is created by Yuan Tao (ewan.msn@gmail.com)
 * Licensed under GNU GPL v3
 * 
 * $Author: ewan.msn@gmail.com $
 * $Date: 2011-10-27 01:23:46 +0000 (Thu, 27 Oct 2011) $
 * $Rev: 43 $
 * $HeadURL: http://comp6471.googlecode.com/svn/Project3/src/Project3Main.java $
 * 
 */

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import UI.UIConsole;

import kklib.*;
import util.*;

public class Project3Main {
	
	public static ArrayList<Layout> layouts = new ArrayList<Layout>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// create and initialize the logger
		logger.init();

		UIConsole ui = new UIConsole();
		
		// show greetings
		if (ui.showGreetings() != 0) {
			ui.showMessage("showGreetings error.");
			return;
		}

		// format the result.txt file.
		SimpleDateFormat tmpDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datetimeNow = tmpDate.format(new java.util.Date());
		logger.output(datetimeNow + "\nThe result is:" + "\n");
		
		// load all txt files
		GameLoader txtLoader = new GameLoader();
		String path = System.getProperty("user.dir") + "\\input";
		if (txtLoader.loadTextFiles(path, layouts) != 0) {
			return;
		}

		// checking
		Layout layout = null;
		for (int i = 0; i < layouts.size(); i++) {
			layout = layouts.get(i);

			// logging
			logger.info(layout.fileFullname);
			logger.info(layout.txtFileData.toString());

			// parse one file
			if (txtLoader.fileDataParser(layout) != 0) {
				ui.showMessage("fileDataParser error, please see logs for more information.");
				return;		// exit
			}
			// check the syntax of the cages and cells.
			if (layout.syntaxChecker() != 0) {
				ui.showMessage("syntaxChecker error, please see logs for more information.");
				return;
			}
			
			// add data to cells for printing
			layout.addDataToCells();
		}
		
		boolean ifExit = false;
		
		while (!ifExit) {
			// show all games
			layout = ui.showAllGamesAndChooseAGame(layouts);
			if (layout == null) {
				// ui.showResult("showAllGamesAndChooseAGame error.");
				break;
			}
			
			// get cell value(s) and check if they are right
			while (true) {
				int ret = ui.getCellValues(layout);
				
				// get a new value
				if (ret == 0) {
					int retSolution = layout.checkSolution();
					
					if (retSolution == 0) {
						ui.showLayout(layout);
						ui.showMessage("Congratulations!");
						break;

					} else if (retSolution == 1) {
						// need to set values for more cells.
						continue;
					
					} else {
						ui.showMessage("Sorry, please check the values of the cells.");
					}
					
				// exit command
				} else if (ret == 1) {
					ifExit = true;
					break;
				
				// input error. try it again.
				} else {
					String msg = "Input error (make sure the name and value of cell are meaningful ";
					msg += "and do not use BACKSPACE or DELETE key), please try it again";
					ui.showMessage(msg);
				}
			}
			
			if (!ifExit && !ui.ifPlayAgain()) {
				break;
			}
		}

		ui.showMessage("The program ends successfully!");
	}

}
